package com.shop.utils;

import com.jfinal.kit.Kv;
import com.jfinal.kit.PathKit;

/**
 * creat_user:  denghuanqing
 * creat_date: 2018/1/5
 * creat_time: 19:33
 * describe: 代码生成器
 **/
public class CodeGenerator {

    protected String packageController = "com.shop.controller";
    protected String packageService = "com.shop.service";
    protected String packageModel = "com.shop.model";

    public CodeGenerator controller(String className) {
        String classNameSmall = className.toLowerCase();

        RenderCode.render("controller.html",
                Kv.by("package", packageController)
                        .set("className", className)
                        .set("classNameSmall", classNameSmall)
                        .set("modelPac", packageModel)
                        .set("servicePac", packageService)
                ,
                new StringBuilder()
                        .append(PathKit.getWebRootPath())
                        .append("/src/main/java/")
                        .append(toPaths(packageController))
                        .append("/")
                        .append(className)
                        .append("Controller.java")
        );
        return this;
    }

    public CodeGenerator service(String className) {
        String classNameSmall = className.toLowerCase();

        RenderCode.render("service.html",
                Kv.by("package", packageService)
                        .set("className", className)
                        .set("classNameSmall", classNameSmall)
                        .set("modelPac", packageModel)
                ,
                new StringBuilder()
                        .append(PathKit.getWebRootPath())
                        .append("/src/main/java/")
                        .append(toPaths(packageService))
                        .append("/")
                        .append(className)
                        .append("Service.java")
        );
        return this;
    }

    private String toPaths(String packageBase) {
        return packageBase.replace(".", "/");
    }

    public static void main(String[] args) {
        /**
         * 1.jfinal自带的代码生成工具生成model
         * 2.生成Service
         * 3.生成Controller
         * 具体项目，具体需求可以自定义模板文件
         */
        new CodeGenerator().service("Blog");
        new CodeGenerator().controller("Blog");
    }
}
